# encoding=utf-8
import numpy, cv2
import face_recognition



def GetFace(image):
    cv0 = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
    cv0.load("D:/opencv/sources/data/haarcascades/haarcascade_frontalface_alt.xml")
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = cv0.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.CASCADE_SCALE_IMAGE
    )
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

cam = cv2.VideoCapture("rtsp://admin:123456@192.168.2.244/video1")
fps = cam.get(cv2.CAP_PROP_FPS)
width=int(cam.get(cv2.CAP_PROP_FRAME_WIDTH)+0.5)
height=int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT)+0.5)
fourcc=cv2.VideoWriter_fourcc(*'mp4v')
out=cv2.VideoWriter('output.mp4',fourcc,20.0,(width,height))
while (cam.isOpened()):
    ret,frame=cam.read()
    if ret==True:
        cv2.imwrite('test111.jpg', frame)  # 存储为图像,保存名为 文件夹名_数字（第几个文件）.jpg
        frame = GetFace(frame)
        if frame != None:
            out.write(frame)
            cv2.imshow("my244",frame)
            break
        if (cv2.waitKey(1)&0xFF)==ord('q'):
            break
out.release()
cam.release()
cv2.destroyAllWindows()